url = 'https://www.acwing.com/problem/content/849/'

from queue import Queue

N = 100010

h = [-1] * N
e = [0] * N
ne = [0] * N
idx = 0
d = [-1] * N

def add(a, b):
    global idx
    e[idx], ne[idx], h[a] = b, h[a], idx
    idx += 1

def bfs():
    global d
    d[1] = 0
    q = Queue()
    q.put(1)

    while not q.empty():
        t = q.get()

        i = h[t]
        while i != -1:
            j = e[i]
            if d[j] == -1:
                d[j] = d[t] + 1
                q.put(j)
            i = ne[i]

    return d[n]

n, m = map(int, input().split())
for i in range(m):
    a, b = map(int, input().split())
    add(a, b)

print(bfs())
